package org.budo.mongo.jdbc.driver.statement.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.budo.jdbc.driver.AbstractBudoJdbcPreparedStatement;
import org.budo.jdbc.driver.resultset.impl.MapArrayResultSet;
import org.budo.mongo.jdbc.driver.executor.MongoStatementExecutor;
import org.budo.mongo.jdbc.driver.statement.AbstractMongoStatement;
import org.budo.support.lang.util.MapUtil;

/**
 * SHOW table status
 * 
 * @author lmw
 */
public class SHOW_TABLE_STATUS extends AbstractMongoStatement {
    private static final String NAME = "Name";

    private static final String ENGINE = "Engine";

    private static final String INNO_DB = "InnoDB";

    private static final String COMMENT = "Comment";

    @Override
    public void execute(PreparedStatement preparedStatement) throws SQLException {
        MongoStatementExecutor statementExecutor = this.statementExecutor(preparedStatement);

        Iterator<?> collectionNames = statementExecutor.listCollectionNames();

        List<Map<?, ?>> maps = new ArrayList<Map<?, ?>>();
        while (collectionNames.hasNext()) {
            String collectionName = (String) collectionNames.next();
            maps.add(MapUtil.stringMap(NAME, collectionName, ENGINE, INNO_DB, COMMENT, ""));
        }

        String[] columnNames = new String[] { NAME, ENGINE, COMMENT };
        ResultSet resultSet = new MapArrayResultSet(columnNames, maps.toArray(new Map[maps.size()]));
        ((AbstractBudoJdbcPreparedStatement) preparedStatement).setResultSet(resultSet);
    }
}